package com.atguigu.atcrowdfunding.manager.controller;

import com.atguigu.atcrowdfunding.util.AjaxResult;
import com.atguigu.atcrowdfunding.util.Page;
import com.sun.deploy.net.HttpResponse;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.DeploymentQuery;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xzt
 * @create 2021-03-11 19:09
 */
@Controller
@RequestMapping("/process")
public class ProcessController {

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private ProcessEngine processEngine;

    @RequestMapping("/index")
    public String index(){

        return "process/index";
    }

    @ResponseBody
    @RequestMapping("/doIndex")
    public Object doIndex(@RequestParam(value = "pageNo",required = false,defaultValue = "1") Integer pageNo,
                          @RequestParam(value = "pageSize",required = false,defaultValue = "10") Integer pageSize){

        AjaxResult ajaxResult = new AjaxResult();
        try {

            Page page = new Page(pageNo,pageSize);
            ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();
            List<ProcessDefinition> listPage = processDefinitionQuery.listPage(page.startIndex(), pageSize);

            List<Map<String,Object>> mylistPage = new ArrayList<>();

            for (ProcessDefinition processDefinition : listPage) {
                HashMap<String, Object> pd = new HashMap<>();
                pd.put("id",processDefinition.getId());
                pd.put("name",processDefinition.getName());
                pd.put("version",processDefinition.getVersion());
                pd.put("key",processDefinition.getKey());
                mylistPage.add(pd);
            }

            Long count = processDefinitionQuery.count();
            page.setDatas(mylistPage);
            page.setTotalSize(count.intValue());

            ajaxResult.setPage(page);
            ajaxResult.setSuccess(true);
            ajaxResult.setMessage("查询流程定义成功");
        }catch (Exception e){
            ajaxResult.setSuccess(true);
            ajaxResult.setMessage("查询流程定义失败");
            e.printStackTrace();
        }
        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping("/deploy")
    public Object doUpload(@RequestParam("processDefFile")MultipartFile file, HttpSession session){
        AjaxResult ajaxResult = new AjaxResult();
        try {
            InputStream inputStream = file.getInputStream();
            repositoryService.createDeployment().addInputStream(file.getOriginalFilename(),inputStream).deploy();

            ajaxResult.setMessage("数据部署成功");
            ajaxResult.setSuccess(true);
        }catch (Exception e){
            ajaxResult.setMessage("数据部署失败");
            ajaxResult.setSuccess(false);
            e.printStackTrace();
        }
        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping("/doDeleteAll")
    public Object doDeleteAll(@RequestParam("id") String id){
        AjaxResult ajaxResult = new AjaxResult();
        try {

            ProcessDefinitionQuery query = repositoryService.createProcessDefinitionQuery();
            ProcessDefinition processDefinition = query.processDefinitionId(id).singleResult();

            repositoryService.deleteDeployment(processDefinition.getDeploymentId(),true);

            ajaxResult.setMessage("流程定义删除成功");
            ajaxResult.setSuccess(true);
        }catch (Exception e){
            ajaxResult.setMessage("流程定义删除失败");
            ajaxResult.setSuccess(false);
            e.printStackTrace();
        }
        return ajaxResult;
    }

    @RequestMapping("/showimg")
    public String showimg(String id){
        return "process/showimg";
    }


    @ResponseBody
    @RequestMapping("/ShowimgProDef")
    public void ShowimgProDef(@RequestParam("id") String id, HttpServletResponse response) throws IOException {
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionId(id).singleResult();
        InputStream resourceAsStream = repositoryService.getResourceAsStream(processDefinition.getDeploymentId(), processDefinition.getDiagramResourceName());

        ServletOutputStream outputStream = response.getOutputStream();

        IOUtils.copy(resourceAsStream,outputStream);
    }

}
